package com.atguigu.dga.governance.assessor.storage;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;


@Component("IS_TABLE_SIMILAR")
public class IsTableSimilarAssessor  extends Assessor {


    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        //参考百分比
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        BigDecimal paramPercent = paramJsonObj.getBigDecimal("percent");
        //相似表清单
        List<String>  sameTableNameList=new ArrayList<>();
        // 1先获取 表元数据
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        // 2 获取其他所有表的元数据
        List<TableMetaInfo> otherTableMetaInfoList = assessParam.getTableMetaInfoList();
        // 3  三层循环 判断   1 其他所有表 2本表的所有字段   3 其他表的所有字段   有几个字段重复
        for (TableMetaInfo otherTableMetaInfo : otherTableMetaInfoList) {
            //跳过自己
            if(otherTableMetaInfo.getId().equals(tableMetaInfo.getId())){
                continue;
            }
            //只保留同层次的
            if (!otherTableMetaInfo.getTableMetaInfoExtra().getDwLevel().equals(tableMetaInfo.getTableMetaInfoExtra().getDwLevel())){
                continue;
            }
            int sameFieldCount = 0;
            List<JSONObject> colJsonObjList = JSON.parseArray(tableMetaInfo.getColNameJson(), JSONObject.class);//本表字段
            List<JSONObject> otherColJsonObjList = JSON.parseArray(otherTableMetaInfo.getColNameJson(), JSONObject.class); //其他表字段
            for (JSONObject colJsonObj : colJsonObjList) {
                for (JSONObject otherColJsonObj : otherColJsonObjList) {
                    if (colJsonObj.getString("name").equals(otherColJsonObj.getString("name"))){
                        sameFieldCount++;  // 相同字段数量++
                    }
                }
            }

            BigDecimal samePercent = BigDecimal.valueOf(sameFieldCount).divide(BigDecimal.valueOf(colJsonObjList.size()), 2, BigDecimal.ROUND_HALF_UP).movePointRight(2);
            //相似百分比如果大于参考百分比 则视为相似表
            if(samePercent.compareTo(paramPercent)>0){
                sameTableNameList.add(otherTableMetaInfo.getTableName());
            }

        }

        //给评价
        if(sameTableNameList.size()>0){
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("存在相似表："+ StringUtils.join(sameTableNameList,",")  );
        }


    }
}
